<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="招生老师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入招生老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="民族" align="center" prop="nationality" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="初中" align="center" prop="middleSchool" />
      <el-table-column label="初中地址" align="center" prop="middleSchoolAddr" />
      <el-table-column label="是否应届生" align="center" prop="freshGraduates">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enroll_student_info" :value="scope.row.freshGraduates"/>
        </template>
      </el-table-column>
      <el-table-column label="邮寄地址" align="center" prop="mailingAddress" />
      <el-table-column label="收件人姓名" align="center" prop="mailingName" />
      <el-table-column label="收件人电话" align="center" prop="mailingMobile" />
      <el-table-column label="意向专业" align="center" prop="majorName" />
      <el-table-column label="招生老师" align="center" prop="teacherName" />
<!--      <template slot-scope="scope">
        {{ getTeacherName(scope.row.teacherId) }}
      </template>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:info:edit']"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="showContactInformation(scope.row)"
          >家庭信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['student:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报名学生基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入出生日期" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="民族" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="初中" prop="middleSchool">
          <el-input v-model="form.middleSchool" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="初中地址" prop="middleSchoolAddr">
          <el-input v-model="form.middleSchoolAddr" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否应届生" prop="freshGraduates">
          <el-select v-model="form.freshGraduates" placeholder="请选择是否应届生">
            <el-option
              v-for="dict in dict.type.enroll_student_info"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮寄地址" prop="mailingAddress">
          <el-input v-model="form.mailingAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="收件人姓名" prop="mailingName">
          <el-input v-model="form.mailingName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="收件人电话" prop="mailingMobile">
          <el-input v-model="form.mailingMobile" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="意向专业" prop="majorId">
          <el-select v-model="form.majorId" placeholder="请选择意向专业" :loading="teacherLoading">
            <el-option
              v-for="major in majorList"
              :key="major.id"
              :label="major.majorName"
              :value="major.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="招生老师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择招生老师" :loading="teacherLoading">
            <el-option
              v-for="teacher in teacherList"
              :key="teacher.id"
              :label="teacher.userName"
              :value="teacher.id"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="openContact"
      title="家庭信息"
      width="800px"
      :center="true">
      <el-table :data="familyInfo">
        <el-table-column label="关系" width="120">
          <template slot-scope="scope">
            {{ scope.row.relationWithSelf }}
          </template>
        </el-table-column>
        <el-table-column label="姓名">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.name"
              :readonly="true"
            />
          </template>
        </el-table-column>
        <el-table-column label="移动电话" width="175">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.mobile"
              placeholder="无"
              :readonly="true"
              style="background-color: #f5f7fa;"
            />
          </template>
        </el-table-column>
        <el-table-column label="QQ号" width="175">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.qq"
              :readonly="true"
            />
          </template>
        </el-table-column>
        <el-table-column label="微信号" width="175">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.wechat"
              :readonly="true"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo,teacherList,majorList,getContact} from "@/api/student/student";

export default {
  name: "Info",
  dicts: ['sys_user_sex', 'enroll_student_info'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //
      openContact:false,
      // 总条数
      total: 0,
      // 报名学生基础信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        teacherId: null,
        teacherName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 招生老师列表
      teacherList: [],
      //专业列表
      majorList: [],
      //专业加载状态
      majorLoading:false,
      // 招生老师加载状态
      teacherLoading: false,
      // 老师ID到名称的映射
      teacherMap: {},
      majorMap: {},
      familyInfo: [],
      contactInfo:{}
    };
  },
  created() {
    this.getList();
    this.getTeacherList(); // 初始化加载老师列表
    this.getMajorList();//初始化学生列表
  },
  methods: {
    /** 查询报名学生基础信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取招生老师列表
    getTeacherList() {
      this.teacherLoading = true;
      teacherList().then(response => {
        this.teacherList = response.data || [];
        // 构建ID到名称的映射表
        this.teacherMap = this.teacherList.reduce((map, teacher) => {
          map[teacher.id] = teacher.userName;
          return map;
        }, {});
        this.teacherLoading = false;
      }).catch(() => {
        this.teacherLoading = false;
      });
    },

    // 获取专业列表
    getMajorList() {
      this.majorLoading = true;
      majorList().then(response => {
        this.majorList = response.data || [];
        // 构建ID到名称的映射表
        this.majorMap = this.majorList.reduce((map, major) => {
          map[major.id] = major.majorName;
          return map;
        }, {});
        this.majorLoading = false;
      }).catch(() => {
        this.majorLoading = false;
      });
    },
    // 根据ID获取老师姓名
    getTeacherName(teacherId) {
      return this.teacherMap[teacherId] || teacherId;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        sex: null,
        age: null,
        birthday: null,
        nativePlace: null,
        nationality: null,
        idCard: null,
        middleSchool: null,
        middleSchoolAddr: null,
        freshGraduates: null,
        mailingAddress: null,
        mailingName: null,
        mailingMobile: null,
        majorId: null,
        teacherId: null,
        teacherName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名学生基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报名学生基础信息";
      });
    },
    /** 家庭信息 */
    showContactInformation(row) {
      getContact(row.id).then(response => {
       this.familyInfo=response.data;
       this.openContact = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报名学生基础信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
